$(document).ready(function($) {
	(function($) {
	$.oValidate = function(id) {
		var list = {
				'reset_password_form':reset_password_form,
				'forget_password_from':forget_password_validate,
				'send_message_form':send_message_validate,
				'create_group_form':create_group_validate,
				'update_group_form':update_group_validate,
				'create_topic_form':create_topic_validate,
				'update_topic_form':update_topic_validate,
				'create_album_form':create_album_validate,
				'update_album_form':update_album_validate,
				'publish_form':publish_form_validate,
				'register_form':register_validate,
				'social_register_form':social_register_validate,
				'update_userinfo':update_userinfo_validate,
				'update_password_form':update_password_form,
				'setting_forum_form':setting_forum_validate,
				'login_form':login_validate,
				'apply_form':apply_validate,
				'sharestore_form':sharestore_validate,
				'theme_select_form':theme_select_validate,
				'update_staruser':update_staruser_validate,
				'bbs_login_form':bbs_login_validate
		};
		if(id&&list[id]&&$('#'+id)) {
			$('#'+id).validate(list[id]);
		}else if(id&&$('#'+id)){
			$('#'+id).validate({});
		}
	};
	
	
	var reset_password_form = {
			rules: {
				new_passwd: { pzrequired: true, rangelength: [6, 15] },
				new_verify_passwd: { pzrequired: true, rangelength: [6, 15], equalTo: "#new_passwd" }
			},
			messages: {
				new_passwd: { pzrequired: tip_required, rangelength: function(){ return $('#new_passwd').attr('not-valid');} },
				new_verify_passwd: { pzrequired: tip_required, rangelength: function(){ return $('#new_passwd').attr('not-valid');},equalTo: function(){ return $('#new_passwd').attr('not-match');} }
			}
		};
	
	var forget_password_validate = {
			rules : {
				email : {pzrequired : true,email : true}
			},
			messages : {
				email : {pzrequired : tip_required,email : function(){ return $('#email').attr('not-valid');}}
			}
	};
	
	var send_message_validate = {
			rules : {message_user : {required : true}, message_content : {byteRangeLength:[0,300]}},
			messages : {message_user : {required : tip_required}}
	};
	
	var create_album_validate = {
			rules : {album_title : {pzrequired : true,byteRangeLength:function(elem){return [$(elem).attr('min-length'),$(elem).attr('max-length')];}},
					album_desc : {byteRangeLength:function(elem){return [$(elem).attr('min-length'),$(elem).attr('max-length')];}}},
			messages : {album_title : {pzrequired : tip_required}}
	};
	
	var update_album_validate = {
			rules : {album_title : {pzrequired : true,byteRangeLength:[3,20]},
				album_desc : {byteRangeLength:[0,200]}},
			messages : {album_title : {pzrequired : tip_required}}
	};
	
	var sharestore_validate = {rules : {store_name : {pzrequired : true}},
								messages : {store_name : {pzrequired : tip_required}}};
	
	var theme_select_validate = {rules : {tmp : {required : true}},
								messages : {tmp : {required : tip_required}}};
	
	var update_staruser_validate = {rules : {star_reason : {required : true,byteRangeLength:[0,300]}},
									messages : {star_reason : {required : tip_required}}};
	
	var create_topic_validate = {
			rules : {	
				topic_title : {required : true,byteRangeLength:[4,30]},
				topic_desc : {byteRangeLength:[0,200]}
				},
			messages : {topic_title : {required : tip_required}}
	};
	
	var update_topic_validate = {
			rules : {topic_title : {required : true,byteRangeLength:[4,30]},
					 topic_desc : {byteRangeLength:[0,200]}},
			messages : {topic_title : {required : tip_required}}
	};
	
	var create_group_validate = {
			rules : {	
				group_title : {required : true,byteRangeLength:[5,50]},
				group_desc : {byteRangeLength:[0,300]}
				},
			messages : {group_title : {required : tip_required}}
	};
	

	var update_group_validate = {
			rules : {	
				group_title : {required : true,byteRangeLength:[5,50]},
				group_desc : {byteRangeLength:[0,300]}
				},
			messages : {	
				group_title : {required : tip_required}}
	};
	
	var apply_validate= {
			rules : {	
				apply_cid : {required : true},
				apply_reason : {required : true,byteRangeLength:[0,300]}
				},
			messages : {	
				apply_cid : {required : tip_required},
				apply_reason : {required : tip_required}
				}
	};

	var publish_form_validate = {
		rules : {title : {pzrequired : true, byteRangeLength:function(elem){return [$(elem).attr('min-length'),$(elem).attr('max-length')];}}},
		messages : {title : {pzrequired : tip_required}},
		submitHandler : function(form) {
			$(form).find('[placeholder]').each(function() {
		        var input = $(this);
		        if (input.val() == input.attr('placeholder')) {
		            input.val('');
		        }
		      });
			var validpass = true;
			//var formdata =  $(form).formSerialize();
			var imagediv = $(form).find('.pzuimg-thumbs');
			var uploadimgs = [];
			var uploadfiles = [];
			var videos = [];
			var fetchs = [];
			var coverfile = [];
			
			imagediv.find('.thumb-upload').each(function(){
				if($(this).hasClass('selected')){
					var cover = $(this).hasClass('cover');
					var file = {aid:$(this).attr('data-aid'),desc:$(this).find('textarea').val(),cover:cover};
					uploadimgs.push(file);
					if(cover) coverfile = file;
				}
			});
			imagediv.find('.thumb-upload-others').each(function(){
				if($(this).hasClass('selected')){
					var file = {aid:$(this).attr('data-aid'),desc:$(this).find('textarea').val(),cover:false};
					uploadfiles.push(file);
				}
			});
			imagediv.find('.thumb-video').each(function(){
				if($(this).hasClass('selected')){
					var cover = $(this).hasClass('cover');
					var file = {url:$(this).attr('data-url'),image:$(this).attr('data-image'),flv:$(this).attr('data-flv'),desc:$(this).find('textarea').val(),cover:cover};
					videos.push(file);
					if(cover) coverfile = file;
				}
			});
			imagediv.find('.thumb-fetch').each(function(){
				if($(this).hasClass('selected')){
					var cover = $(this).hasClass('cover');
					//var file = {url:$(this).attr('data-url'),image:$(this).attr('data-image'),type:$(this).attr('data-type'),prourl:$(this).attr('data-prourl'),desc:$(this).find('textarea').val(),cover:cover};
					var file = {url:$(this).attr('data-url'),itemid:$(this).attr('data-itemid'),image:$(this).attr('data-image'),type:$(this).attr('data-type'),desc:$(this).find('textarea').val(),cover:cover,prourl:$(this).attr('data-prourl')};
					fetchs.push(file);
					if(cover) coverfile = file;
				}
			});
			/*console.log('uploadimgs:'+uploadimgs.length);
			console.log('videos:'+videos.length);
			console.log('fetchs:'+fetchs.length);
			console.log('uploadfiles:'+uploadfiles.length);*/
			var needconfirm = (coverfile.length==0&&(uploadimgs.length>0||videos.length>0||fetchs.length>0))?true:false;
			if(needconfirm){
				var c = confirm(tip_no_img_cover);
				if(!c) return false;
			}
			/*formdata['uploadimgs'] = serialize(uploadimgs);
			formdata['uploadfiles'] = serialize(uploadfiles);
			formdata['videos'] = serialize(videos);
			formdata['fetchs'] = serialize(fetchs);
			formdata['text'] = $.oFEditor($(form).attr('data-editorid')).getSubmitValue();*/
			$(form).find('input[name="uploadimgs"]').val(serialize(uploadimgs));
			$(form).find('input[name="uploadfiles"]').val(serialize(uploadfiles));
			$(form).find('input[name="videos"]').val(serialize(videos));
			$(form).find('input[name="fetchs"]').val(serialize(fetchs));
			$(form).find('input[name="text"]').val($.oFEditor($(form).attr('data-editorid')).getSubmitValue());
			var album_select_id = $(form).find('.album_select_id').val();
			if (album_select_id == null || album_select_id == '' || album_select_id == 0) {
				show_message('error',tip_album_cannot_be_null,false,0);
				return false;
			}
			//console.log(formdata);
			$(form).ajaxSubmit({
				url : $(form).attr('action'),
				type : 'POST',
				dataType : 'json',
				beforeSubmit : function() {
					$(form).find('.submit-btn').button('loading');
				},
				success : function(result) {
					$(form).find('.submit-btn').button('reset');
					if (result.success === !0) {
						show_message('success',result.message,false,0);
						setTimeout(function(){
							window.location.href = result.data.url;
							//window.location.reload();
						},1000);
					} else {
						show_message('error',tip_error+result.message,false,0);
					}
				},
				error : function(e) {
					checkredirect(e);
					$(form).find('.submit-btn').button('reset');
				}
			});
			return false;
		}
	};
	
	var update_userinfo_validate = {
		rules: {
			nickname: { pzrequired: true,byteRangeLength:[4,20]},
			domain: { byteRangeLength:[0,7]}
		},
		messages: {
			nickname: { pzrequired: tip_required, byteRangeLength: function(){ return $('#nickname').attr('not-valid');},remote: function(){return $('#nickname').attr('remote-error');}},
			domain: { remote: function(){ return $('#domain').attr('remote-error');}}
		}
	};
	
	var setting_forum_validate = {
			rules: {
				bbs_username: { pzrequired: true },
				bbs_password: { pzrequired: true }
			},
			messages: {
				bbs_username: { pzrequired: tip_required},
				bbs_password: { pzrequired: tip_required}
			}
		};
	
	var update_password_form = {
			rules: {
				email: { pzrequired: true, email: true},
				org_passwd: { pzrequired: true, rangelength: [6, 15] },
				new_passwd: { pzrequired: true, rangelength: [6, 15] },
				new_verify_passwd: { pzrequired: true, rangelength: [6, 15], equalTo: "#new_passwd" }
			},
			messages: {
				email: { pzrequired: tip_required,email: function(){ return $('#email').attr('not-valid');}, remote: function(){ return $('#email').attr('remote-error');}},
				org_passwd: { pzrequired: tip_required, rangelength: function(){ return $('#new_passwd').attr('not-valid');}},
				new_passwd: { pzrequired: tip_required, rangelength: function(){ return $('#new_passwd').attr('not-valid');}},
				new_verify_passwd: { pzrequired: tip_required, rangelength: function(){ return $('#new_passwd').attr('not-valid');},equalTo: function(){ return $('#new_passwd').attr('not-match');} }
			}
		};

	var login_validate = {
		rules : {
			email : {required : true,email : true},
			password : {required : true,minlength : 6},
			verifycode: { pzrequired: true}
		},
		messages : {
			email : {required : tip_required,email : function(){ return $('#email').attr('not-valid');}},
			password : {required : tip_required,minlength : function(){ return $('#password').attr('not-valid');}},
			verifycode: { pzrequired: tip_required,remote: function(){ return $('#verifycode').attr('remote-error');}}
		}
	};
	var bbs_login_validate = {
			rules : {bbs_username : {pzrequired:true},bbs_password : {pzrequired:true}},
			messages : {bbs_username : {required : tip_required},bbs_password : {required : tip_required}}
		};
	
	var register_validate = {
		rules: {
			nickname: { pzrequired: true,byteRangeLength:[4,20]},
			email: { pzrequired: true, email: true},
			verifycode: { pzrequired: true},
			password: { pzrequired: true, rangelength: [6, 15] },
			passconf: { pzrequired: true, rangelength: [6, 15],equalTo: "#password" },
			terms: {"required":true}
		},
		messages: {
			nickname: { pzrequired: tip_required, byteRangeLength: function(){ return $('#nickname').attr('not-valid');},remote: function(){return $('#nickname').attr('remote-error');}},
			email: { pzrequired: tip_required,email: function(){ return $('#email').attr('not-valid');}, remote: function(){ return $('#email').attr('remote-error');}},
			verifycode: { pzrequired: tip_required, remote: function(){ return $('#verifycode').attr('remote-error');}},
			password: { pzrequired: tip_required, rangelength: function(){ return $('#password').attr('not-valid');}},
			passconf: { pzrequired: tip_required, rangelength: function(){ return $('#password').attr('not-valid');},equalTo: function(){ return $('#passconf').attr('not-match');} },
			terms:  {"required": tip_required}
		}
	};
	
	var social_register_validate = {
			rules: {nickname:{pzrequired:true,byteRangeLength:[4,20]}},
			messages: {nickname: { pzrequired: tip_required, byteRangeLength: function(){ return $('#nickname').attr('not-valid');},remote: function(){return $('#nickname').attr('remote-error');}}}
		};
	
})(jQuery);
		jQuery.validator.addMethod("byteRangeLength", function(value, element, param) {      
		  var length = value.length;      
		  for(var i = 0; i < value.length; i++){      
		      if(value.charCodeAt(i) > 127){      
		      length++;      
		      }      
		  }
		  return this.optional(element) || ( length >= param[0] && length <= param[1] );      
		}, tip_length_rang); 
		
		
		jQuery.validator.addMethod("notDigit", function(value, element, param) {
				var patrn=/^[0-9]{1,20}$/; 
				if(patrn.test(value))   return false; 
				return true; 
			}, tip_not_all_digit); 

		jQuery.validator.addMethod("pzrequired", function(value, element, param) {
				//console.log('isInputEmpty'+isInputEmpty($(element)));
				if(isInputEmpty($(element)))   return false; 
				return true; 
			}, tip_required);
		
		jQuery.validator.setDefaults({
			    errorPlacement: function(error, placement) {
			      $(placement).attr('data-original-title',error.text());
			      $(placement).tooltip({
			         title: error.text(),
			         placement: 'top',
			         template: '<div class="tooltip"><div class="tooltip-error-arrow"></div><div class="tooltip-inner error-tip"></div></div>',
			         trigger:'manual'
			      });
			      $(placement).tooltip('show');
			      $(placement).bind('keydown',function(){$(placement).tooltip('hide');});
			   },
			   onkeyup: false,
			   focusInvalid: false,
			   submitHandler : function(form) {
				   $(form).find('[placeholder]').each(function() {
				        var input = $(this);
				        if (input.val() == input.attr('placeholder')) {
				            input.val('');
				        }
				      });
					$(form).ajaxSubmit({
							url : $(form).attr('action'),
							type : 'POST',
							dataType : 'json',
							beforeSubmit: function(){
								$('.submit-btn').button('loading');
							},
							success: function(result) {
								$('.submit-btn').button('reset');
								if (result.success === !0) {
									show_message('success',result.message,true,1000);
									setTimeout(function(){
										if(result.data&&result.data.url){
											window.location.href = result.data.url;
										}else if(result.data&&result.data.jsfunc){
											eval(result.data.jsfunc)(result);
										}else{
											window.location.reload();
										}
									},1000);
									return true;
								} else {
									showError(result.message);
									return false;
								}
							},
							error:function(data){
								checkredirect(data);
								$('.submit-btn').button('reset');
							}
					});
					return false;
				}
			});
		
});